package gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;

import weka.core.Instances;
import dataEntity.ClassifierData;

public class TestFrame extends JFrame{
	private static final long serialVersionUID = 1L;
	InputPanel ip = new InputPanel();
	JButton jb = new JButton("Check");
	List<ClassifierData> classifiers = new ArrayList<ClassifierData>();
	List<JCheckBoxMenuItem> classifiersCheckBox = new ArrayList<JCheckBoxMenuItem>();
	public TestFrame(){
		try {
			File f = new File("./objects");
			f.isDirectory();
			for(File file : f.listFiles()){
				if(file.isFile()){
					ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
					ClassifierData cd = (ClassifierData) ois.readObject();
					classifiers.add(cd);
					ois.close();
				}
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		jb.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				classify();
			}
		});
		JMenuBar menuBar = new JMenuBar();
		this.setJMenuBar(menuBar);
		JMenu menu = new JMenu("Models");
		menuBar.add(menu);
		for(ClassifierData classifier : classifiers){
			JCheckBoxMenuItem cbMenuItem = new JCheckBoxMenuItem(classifier.getName());
			menu.add(cbMenuItem);
			classifiersCheckBox.add(cbMenuItem);
		}
		add(ip);
		add(jb,BorderLayout.SOUTH);
		pack();
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	public void classify(){
		if(ip.valid()){
			StringBuilder sb = new StringBuilder();
			sb.append("@relation cancer-weather").append("\n");
			sb.append("@attribute getEcol_Rpt_Ind {Y,N}").append("\n");
			sb.append("@attribute getSalm_Rpt_Ind {Y,N}").append("\n");
			sb.append("@attribute getShig_Rpt_Ind {Y,N}").append("\n");
			sb.append("@attribute getCarbon_Monoxide_Ind {Y,N}").append("\n");
			sb.append("@attribute getLead_Ind {Y,N}").append("\n");
			sb.append("@attribute getNitrogen_Dioxide_Ind {Y,N}").append("\n");
			sb.append("@attribute getOzone_Ind {Y,N}").append("\n");
			sb.append("@attribute getParticulate_Matter_Ind {Y,N}").append("\n");
			sb.append("@attribute getSulfur_Dioxide_Ind {Y,N}").append("\n");
			sb.append("@attribute getToxic_Chem numeric").append("\n");
			sb.append("@attribute precipitationInches numeric").append("\n");
			sb.append("@attribute cancerLevelClass {1,2,3}").append("\n");
			sb.append("\n");
			sb.append("@data").append("\n");
			sb.append(ip.toString()).append(",?");
			
			Instances i;
			try {
				i = new Instances(new BufferedReader(new StringReader(sb.toString())));
				i.setClassIndex(i.numAttributes() - 1);
				StringBuilder dialogMessage = new StringBuilder();
				dialogMessage.append("This data was classified by the following models:\n");
				boolean selected = false;
				for(int k = 0; k < classifiersCheckBox.size(); k++){
					if(classifiersCheckBox.get(k).isSelected()){
						dialogMessage.append(classifiers.get(k).getName() + "\n          ");
						int classified = (int)classifiers.get(k).getClassifier().classifyInstance(i.instance(0));
						dialogMessage.append(classified == 0 ? "Low" : classified == 1 ? "Medium" : classified == 2 ? "High":"Unclassified");
						dialogMessage.append(" Cancer Rate\n");
						selected = true;
					}
				}
				if(!selected)
					dialogMessage.append("Please select a classifier from the models menu");
				JOptionPane.showMessageDialog(this, dialogMessage.toString(), "Classified As", 1, null);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	public static void main(String[] args) {new TestFrame();}
}
